
module.exports = options =>{
    const jwt = require('jsonwebtoken')
    const assert = require('http-assert')
    const AdminUser = require('../models/AdminUser')
    
    return async(req,res,next)=>{
        // console.log(req,res,next);
        const token = String(req.headers.authorization || '').split(' ').pop()
        // console.log(token);
        assert(token,401,'请先登录')
        const {id} = jwt.verify(token,req.app.get('secret'))
        // console.log(id);
        assert(id,401,'请先登录')
        req.user = await AdminUser.findById(id)
        // console.log(id);
        assert(req.user,401,'请先登录')
        await next()
    }
}